package com.iv.durian.cloud.api;

import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.Notice;
import com.iv.durian.cloud.Notify;
import com.iv.durian.cloud.api.service.NoticeService;
import com.iv.durian.cloud.vo.NoticeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/notice")
@Slf4j
@Api(value = "NoticeApi", description = "NoticeApi", tags = {"notice"})
public class NoticeApi {

    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "创建公告")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(
            @ApiParam(name = "title", value = "标题", required = true) @RequestParam(value = "title") String title,
            @ApiParam(name = "image", value = "封面图片", required = true) @RequestParam(value = "image") String image,
            @ApiParam(name = "sequence", value = "编号", required = true) @RequestParam(value = "sequence") int sequence,
            @ApiParam(name = "lession_id", value = "课程Id", required = true) @RequestParam(value = "lession_id") int lession_id,
            @ApiParam(name = "content", value = "内容") @RequestParam(value = "content") String content
    ) {
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setImage(image);
        notice.setSequence(sequence);
        notice.setLessionId(lession_id);
        notice.setContent(content);

        noticeService.addNotice(notice);
    }


    @ApiOperation(value = "公告列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<List<NoticeVO>>> list(
            @ApiParam(required = false, name = "start", value = "分页起始值") @RequestParam(value = "start", required = false) Integer start,
            @ApiParam(required = false, name = "max", value = "每页记录数") @RequestParam(value = "max", required = false) Integer max,
            @ApiParam(required = false, name = "lession_id", value = "课程id") @RequestParam(value = "lession_id", required = false) Integer lession_id
    ) {
        JsonResult r = new JsonResult();
        try {

            List<Notice> noticeList = noticeService.getList(lession_id, start, max);
            List<NoticeVO> noticeVOList = noticeList.stream().map(notice -> {
               NoticeVO noticeVO = NoticeVO.builder()
                       .id(notice.getId())
                       .title(notice.getTitle())
                       .image(notice.getImage())
                       .sequence(notice.getSequence())
                       .lessionId(notice.getLessionId())
                       .content(notice.getContent())
                       .createTime(notice.getCreateTime())
                       .updateTime(notice.getUpdateTime()).build();
               return noticeVO;
            }).collect(Collectors.toList());

            r.ok(noticeVOList);
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("公告列表信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取公告数量")
    @RequestMapping(value = "/count", method = RequestMethod.POST)
    public ResponseEntity<JsonResult<Integer>> count(
            @ApiParam(required = false, name = "lession_id", value = "课程Id") @RequestParam(value = "lession_id", required = false) Integer lession_id
    ) {
        JsonResult r = new JsonResult();

        try {
            Notice notice = new Notice();
            notice.setLessionId(lession_id);
            r.ok(noticeService.count(notice));
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("公告数量查询失败", e);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "获取公告详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<Notice>> get(
            @ApiParam(required = true, name = "id", value = "公告id") @PathVariable("id") int id
    ) {
        JsonResult r = new JsonResult();

        try {
            Notice notice = noticeService.getNoticeById(id);
            if (notice != null) {
                NoticeVO userVO = NoticeVO.builder()
                        .id(notice.getId())
                        .title(notice.getTitle())
                        .image(notice.getImage())
                        .sequence(notice.getSequence())
                        .lessionId(notice.getLessionId())
                        .content(notice.getContent())
                        .createTime(notice.getCreateTime())
                        .updateTime(notice.getUpdateTime()).build();
                r.ok(userVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("公告信息查询失败", e);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "删除公告")
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public void remove(
            @ApiParam(required = true, name = "id", value = "公告id") @PathVariable("id") int id
    ) {
        log.debug("打印删除的公告id:" + id);
        noticeService.removeNoticeById(id);
    }

    @ApiOperation(value = "修改公告")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(
            @ApiParam(required = true, name = "id", value = "公告id") @RequestParam(value = "id") int id,
            @ApiParam(name = "title", value = "标题") @RequestParam(value = "title", required = false) String title,
            @ApiParam(name = "image", value = "封面图片") @RequestParam(value = "image", required = false) String image,
            @ApiParam(name = "sequence", value = "编号") @RequestParam(value = "sequence", required = false) Integer sequence,
            @ApiParam(name = "lession_id", value = "课程Id") @RequestParam(value = "lession_id", required = false) Integer lession_id,
            @ApiParam(name = "content", value = "内容") @RequestParam(value = "content", required = false) String content
    ) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setTitle(title);
        notice.setImage(image);
        notice.setSequence(sequence);
        notice.setLessionId(lession_id);
        notice.setContent(content);

        noticeService.updateNotice(notice);
    }

}
